<template>
  <div>
    <!--头部-->
   <div class="header-container">
    <div class="header">
      <div class="header-left">
        <img src="@/assets/logo/logo.png" alt="">
        <div class="titles" @click="$router.push('/index/home')">知识博客论坛</div>
      </div>
      <div class="header-center">
        <div class="front-header-nav">
          <el-menu :default-active="$route.path" mode="horizontal" router>
            <el-menu-item index="/index/home">首页</el-menu-item>
            <el-menu-item index="/index/latest">最新博客</el-menu-item>
            <el-menu-item index="/index/myCollect">我的收藏</el-menu-item>
            <el-menu-item index="/index/myBlog">我的博客</el-menu-item>
            <el-menu-item index="/index/profile">个人中心</el-menu-item>
          </el-menu>
        </div>
      </div>
      <div class="header-right">
        <div v-if="!nickName">
          <el-button @click="$router.push('/login')">登录</el-button>
          <el-button @click="$router.push('/register')">注册</el-button>
        </div>
        <div v-else>
          <el-dropdown>
            <div class="header-dropdown">
              <img :src="avatar" alt="">
              <div style="margin-left: 10px; color: #fff">
                <span>{{ nickName }}</span><i class="el-icon-arrow-down" style="margin-left: 5px"></i>
              </div>
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <div style="text-decoration: none" @click="logout">退出</div>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          </div>
        </div>
      </div>
    </div>


    <div>
      <router-view/>
    </div>
  </div>

</template>

<script>

import {mapGetters} from "vuex";
import {getUser} from "@/api/system/user";

export default {
  name: "homePage",
  data() {
    return {
      nickName: null,
      top: '',
      notice: [],
      user: JSON.parse(localStorage.getItem("xm-user") || '{}'),
      title: this.$route.query.title
    }
  },
  created() {
    this.getList();
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'device'
    ]),
  },
  methods: {
    getList() {

      getUser(this.$store.state.user.id).then(res => {
        this.nickName = res.data.nickName
      })
    },
    async logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = '/index';
        })
      }).catch(() => {});
    }
  }

}
</script>
